#include "StdAfx.h"


BBString::BBString(void)
{
}
BBString::~BBString(void)
{
}
//all constructors make sure that the chars passed are converted to TCHAR
BBString::BBString(const char* ccp)
{
	convert(ccp);
}
BBString::BBString(const wchar_t* cwcp)
{
	convert(cwcp);
}
BBString::BBString(const BBString& str)
{
	convert(str.c_str());
}
BBString::BBString(const std::string &str)
{
	convert(str.c_str());
}

BBString::operator const wchar_t*()
{
	return c_str();
}
BBString::operator const char*()
{
	//store the std::string in a local variable and pass a pointer to its c_str
	mbCopy=(std::string)(*this);
	return mbCopy.c_str();
}
//all assignment operators just copy the content of the string into this
BBString& BBString::operator =(const BBString& str)
{
	resize(str.length());
	std::copy(str.begin(), str.end(), begin());
	return *this;
}
//case insensitive comparison
bool BBString::operator ^(BBString &str)
{
	BBString str_1, str_2;
	//convert both to lowercase
	str_1=this->toLower();
	str_2=str.toLower();
	//compare
	return (str_1==str_2);
}
bool BBString::operator %(BBString &str)
{
	BBString str_1, str_2;
	//get case insensitive strings
	str_1=this->toLower();
	str_2=str.toLower();
	//keep comparing the chars until str_1 ends
	for(unsigned int i=0;i<str_1.length();i++)
		if(i>=str_2.length()
			||str_1[i]!=str_2[i])
			return false;
	return true;
}
BBString BBString::toLower()
{
	std::wstring str(c_str());
	str.reserve(size());
	//std::transform(begin(), end(), str.begin(), ::towlower);
	boost::to_lower(str);
	return BBString(str.c_str());
}
BBString BBString::toUpper()
{
	BBString str;
	std::transform(begin(), end(), str.begin(), ::towupper);
	return str;
}
bool BBString::operator ==(BBString &str)
{
	if(size()==str.size())
	{
		for(unsigned int i=0;i<size();i++)
			if(at(i)!=str.at(i))
				return false;
		return true;
	}
	return false;
}
bool BBString::operator !=(BBString &str)
{
	return !(*this==str);
}
//add operators
BBString BBString::operator +(BBString &str)
{
	BBString new_str;
	new_str=*this;
	new_str.append(str);
	return new_str;
}
BBString& BBString::operator +=(BBString &str)
{
	*this=*this+str;
	return *this;
}
BBString operator <<(BBString str, const char* ccp)
{
	str+=string(ccp);
	return str;
}
BBString& operator <<=(BBString& str, const char* ccp)
{
	str+=string(ccp);
	return str;
}
#pragma warning(disable:4996)
//int to alpha and back coversions
BBString BBString::intToAlpha(int n)
{
	char buffer[33];
	std::string str=std::string(itoa(n, buffer, 10));
	return BBString(std::string(str));
}
int BBString::alphaToInt(BBString& str)
{
	return atoi(((std::string)str).c_str());
}
real BBString::alphaToReal(BBString &str)
{
	return (real)atof(((std::string)str).c_str());
}
//conversion functions
//uses old C functions, thus warnings should be disabled
void BBString::convert(const char* ccp)
{
	if(ccp)
	{
		std::string str(ccp);
		resize(str.size());
#ifndef UNICODE
		std::copy(str.begin(), str.end(),begin());
#else
		wchar_t *arr=new wchar_t[str.size()];
		mbstowcs(arr, str.c_str(), str.size());
		std::wstring wstr(arr);
		wstr.resize(str.size());
		std::copy(wstr.begin(), wstr.end(), begin());
		delete[] arr;
#endif
	}
}
void BBString::convert(const wchar_t* cwcp)
{
	if(cwcp)
	{
		std::wstring wstr(cwcp);
		resize(wstr.size());
	#ifdef UNICODE
			std::copy(wstr.begin(), wstr.end(),begin());
	#else
		char *arr=new char[wstr.size()];
		wcstombs(arr, wstr.c_str(), wstr.size());
		std::string str(arr);
		std::copy(str.begin(), str.end(), begin());
		delete[] arr;
	#endif
	}
}
BBString::operator std::string()
{
	std::string str;
#ifdef UNICODE
	char *arr=new char[size()];
	const wchar_t *warr=c_str();
	wcstombs(arr, c_str(), size());
	str=std::string(arr);
	str.resize(size());
	delete[] arr;
#else
	str=std::string(c_str());
#endif
	return str;
}
bool BBString::isWide()
{
	return sizeof(TCHAR)>1;
}
